package com.example.hotel;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Shopping extends Activity {
	String image_shop = "";
	String detail_shop = "";
	String price_shop = "";
	String id_shop = "";
	ImageView image,home;
	TextView txt_shop, price, wellcome, name_shop;
	Button buy;
	URL url;
	Bitmap imag = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		TextView tvAmount = (TextView)findViewById(R.id.tvAmount);
		tvAmount.setText("("+((Global)getApplicationContext()).cart_count.toString()+")");
		image = (ImageView) findViewById(R.id.image_shop);
		home =(ImageView) findViewById(R.id.imagehome);
		txt_shop = (TextView) findViewById(R.id.detail_shop);
		name_shop = (TextView) findViewById(R.id.name_shop);
		buy = (Button) findViewById(R.id.buy);
		price = (TextView) findViewById(R.id.price);
		wellcome = (TextView) findViewById(R.id.tvWellcome);
		wellcome.setText(((Global) getApplicationContext()).wellcome);
		buy.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				((Global) getApplicationContext()).type_cart = "shopping";
				((Global) getApplicationContext()).price_product = price_shop;
				((Global) getApplicationContext()).id_shopping = id_shop;
				
				getAdd_cart();
				((Global) getApplicationContext()).nameCart = "show_cart";
				startActivity(new Intent(Shopping.this, Add_cart.class));
			}
		});
		home.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				// TODO Auto-generated method stub	
				startActivity(new Intent(Shopping.this, home.class));
				wellcome.setText(((Global) getApplicationContext()).wellcome);
			}
		});
		
		getShopping_detail();
		try {

			url = new URL(image_shop);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			imag = BitmapFactory.decodeStream(url.openStream());

		} catch (IOException e) {
			e.printStackTrace();
		}
		image.setImageBitmap(imag);
		txt_shop.setText(Html.fromHtml(detail_shop));
		price.setText(price_shop);
		String str = ((Global) getApplicationContext()).nameShop;
		name_shop.setText(str);
		
		/*------BackHome------*/
		ImageView back_home = (ImageView) findViewById(R.id.imagehome);
		back_home.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {

				startActivity(new Intent(Shopping.this, home.class));
			}
		});
		/*---------------------*/
		
		/*------setfonts------*/
		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount =(TextView)findViewById(R.id.tvAmount);
		txt_shop.setTypeface(Font);
		price.setTypeface(FontBold);
		name_shop.setTypeface(FontBold);
		tv_amount.setTypeface(Font);
		wellcome.setTypeface(FontBold);
		/*--------------------*/
		

	}

	public void getShopping_detail() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ "shopping_detail";
		String NAMESPACE = "";
		String METHOD_NAME = "shopping_detail";
		String imei = ((Global) getApplicationContext()).imei;
		String lang = ((Global) getApplicationContext()).lang;
		String product_id = ((Global) getApplicationContext()).id_shopping;
		// String imei = "410412150911850";
		String str = "";

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		request.addProperty("product_id", product_id);
		// request.addProperty("room","0001");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			String[] a = b.split("\\|");
			indexSub = a[a.length - 1].indexOf("}");
			b = a[a.length - 1].substring(0, indexSub - 2);
			a[a.length - 1] = b;
			for (int i = 0; i < a.length; i++) {
				String[] tmp = a[i].split("\\*");
				id_shop = tmp[0];
				detail_shop = tmp[1];
				price_shop = tmp[2];
				image_shop = tmp[3];
			}

		} catch (Exception e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}

	}
	public void getAdd_cart() {
		String URL1 = ((Global) getApplicationContext()).url;
		String nameCart = ((Global) getApplicationContext()).nameCart;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction + nameCart;
		String NAMESPACE = "";
		String METHOD_NAME = "add_cart";
		String lang = ((Global) getApplicationContext()).lang;
		String imei = ((Global) getApplicationContext()).imei;
		String product_id = ((Global) getApplicationContext()).id_shopping;
		String type_cart = ((Global) getApplicationContext()).type_cart;
		String price = ((Global) getApplicationContext()).price_product;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		request.addProperty("product_id", product_id);
		request.addProperty("type", type_cart);
		request.addProperty("price", price);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

}
